package com.xuexiang.xuidemo.ability;

import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.StackLayout;
import ohos.agp.components.element.PixelMapElement;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.global.resource.NotExistException;

import com.xuexiang.xuidemo.ResourceTable;

import java.io.IOException;

public class SplashAbility extends BaseStatusAbility {

    public final static String KEY_IS_DISPLAY = "key_is_display";
    public final static String KEY_ENABLE_ALPHA_ANIM = "key_enable_alpha_anim";
    /**
     * 默认启动页过渡时间
     */
    private static final int DEFAULT_SPLASH_DURATION_MILLIS = 500;

    private boolean isDisplay = false;
    private ComponentContainer root;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        isDisplay = getIntent().getBooleanParam(KEY_IS_DISPLAY, isDisplay);
        boolean enableAlphaAnim = getIntent().getBooleanParam(KEY_ENABLE_ALPHA_ANIM, false);

        if (enableAlphaAnim) {
            root = new StackLayout(getContext());
            root.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
                    ComponentContainer.LayoutConfig.MATCH_PARENT));
            super.setUIContent(root);

            Image image = new Image(getContext());
            try {
                image.setImageElement(new PixelMapElement(getResourceManager().getResource(ResourceTable.Media_bg_splash)));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NotExistException e) {
                e.printStackTrace();
            }
            image.setScaleMode(Image.ScaleMode.STRETCH);
            image.setLayoutConfig(new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT, StackLayout.LayoutConfig.MATCH_PARENT));
            root.addComponent(image);
            root.setAlpha(0.2f);
        } else {
            super.setUIContent(ResourceTable.Layout_logo_layout);
            root = (ComponentContainer) findComponentById(ResourceTable.Id_logo_layout);
        }

        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
            @Override
            public void run() {
                if (enableAlphaAnim){
                    startSplashAnim(0.2f,1f);
                }else {
                    startSplashAnim(1f,1f);
                }

            }
        }, 500);

    }

    private void startSplashAnim(float start, float end) {
        AnimatorProperty animatorProperty = root.createAnimatorProperty();
        animatorProperty
                .setDuration(DEFAULT_SPLASH_DURATION_MILLIS)
                .alphaFrom(start)
                .alpha(end)
                .setStateChangedListener(new Animator.StateChangedListener() {
                    @Override
                    public void onStart(Animator animator) {

                    }

                    @Override
                    public void onStop(Animator animator) {

                    }

                    @Override
                    public void onCancel(Animator animator) {

                    }

                    @Override
                    public void onEnd(Animator animator) {
                        onSplashFinished();
                    }

                    @Override
                    public void onPause(Animator animator) {

                    }

                    @Override
                    public void onResume(Animator animator) {

                    }
                })
                .start();
    }

    private void onSplashFinished() {
        terminateAbility();
    }

}
